package com.jeeplus.modules.web.wx;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.web.entity.*;
import com.jeeplus.modules.web.mapper.WxBannerMapper;
import com.jeeplus.modules.web.mapper.WxUserMapper;
import com.jeeplus.modules.web.mapper.YjEquipmentMapper;
import com.jeeplus.modules.web.service.WxAgentsEquipmentService;
import com.jeeplus.modules.web.service.WxAgentsService;
import com.jeeplus.modules.web.service.WxCombinationService;
import com.jeeplus.modules.web.service.YjEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongj on 2017/8/6.
 */
@Controller
@RequestMapping(value = "/wx/home")
public class YjBannerController extends BaseController {
    @Autowired
    WxUserMapper wxUserMapper;
    @Autowired
    WxBannerMapper wxBannerMapper;
    @Autowired
    private YjEquipmentMapper equipmentMapper;
    @Autowired
    WxCombinationService wxCombinationService;
    @Autowired
    private WxAgentsEquipmentService wxAgentsEquipmentService;
    @Autowired
    private YjEquipmentService yjEquipmentService;
    @Autowired
    private WxAgentsService wxAgentsService;



    /**
     *
     * @return
     */
    @RequestMapping("/index")
    @ResponseBody
    public Map<String,Object> index( ){
        Map<String,Object> map=new HashMap<String, Object>();
        WxBanner wxBanner=new WxBanner();
        List<WxBanner> wxBannerList=wxBannerMapper.findList(wxBanner);
        List<WxAgents> byLevel = wxAgentsService.getByLevel(0, "1");
        List<WxCombination> wxCombinationList = new ArrayList<>();
        for (int i = 0; i < byLevel.size(); i++) {
            wxCombinationList.addAll(wxCombinationService.findWxCombination(byLevel.get(i).getId(),0));
        }
        //获取总部的套餐信息
        map.put("wxBannerList",wxBannerList);
        map.put("wxCombinationList",wxCombinationList);

        return  map;
    }

    @RequestMapping("/combination")
    @ResponseBody
    public List<WxCombination> combination(String equipment,Integer type){
        List<WxCombination> wxCombinations = new ArrayList<>();
        if(type == null){
            type = 0;
        }

        YjEquipment yjEquipment = yjEquipmentService.findUniqueByProperty("number", equipment);
        if(yjEquipment!=null){
            WxAgentsEquipment wxAgentsEquipment = wxAgentsEquipmentService.findUniqueByProperty("equipment_id", yjEquipment.getId());
            if(wxAgentsEquipment!=null){
                wxCombinations = wxCombinationService.findWxCombination(wxAgentsEquipment.getAgents().getId(),type,yjEquipment.getDeviceType());
                if(wxCombinations.size() == 0 && type==1){
                    List<WxAgents> byLevel = wxAgentsService.getByLevel(0, "1");
                    for (int i = 0; i < byLevel.size(); i++) {
                        wxCombinations.addAll(wxCombinationService.findWxCombination(byLevel.get(i).getId(),type,yjEquipment.getDeviceType()));
                    }
                }
            }
        }
        logger.debug("设备编号:"+equipment+",设备套餐:"+ JSONObject.toJSONString(wxCombinations));
        return wxCombinations;
    }

    @RequestMapping("/newCombination")
    @ResponseBody
    public Map<String,Object> newCombination(String equipment,Integer type){

        List<WxCombination> wxCombinations = new ArrayList<>();
        if(type == null){
            type = 0;
        }

        YjEquipment yjEquipment = yjEquipmentService.findUniqueByProperty("number", equipment);
        if(yjEquipment!=null){
            WxAgentsEquipment wxAgentsEquipment = wxAgentsEquipmentService.findUniqueByProperty("equipment_id", yjEquipment.getId());
            if(wxAgentsEquipment!=null){
                wxCombinations = wxCombinationService.findWxCombination(wxAgentsEquipment.getAgents().getId(),type,yjEquipment.getDeviceType());
                if(wxCombinations.size() == 0 && type==1){
                    List<WxAgents> byLevel = wxAgentsService.getByLevel(0, "1");
                    for (int i = 0; i < byLevel.size(); i++) {
                        wxCombinations.addAll(wxCombinationService.findWxCombination(byLevel.get(i).getId(),type,yjEquipment.getDeviceType()));
                    }
                }
            }
        }
        logger.debug("设备编号:"+equipment+",设备套餐:"+ JSONObject.toJSONString(wxCombinations));
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("wxCombinationList",wxCombinations);
        map.put("device",yjEquipment);
        return map;
    }

    @RequestMapping(value = "log", method= RequestMethod.POST)
    @ResponseBody
    public String log(YjLog yjLog) {
        equipmentMapper.saveLog(yjLog);
        return "success";

    }

}
